/**
 * 创建上传对象
 * @author:szy
 */
define(function (require, exports, module) {
    require("swfupload")
    require("swfupload.queue")

    //拖动文件上传处理属性
    function fileHandle(file) {
        file.id = getHashCode(file.name);
        return file;
    }

    //字符哈希值
    function getHashCode(str) {
        var hash = 1315423911, i, ch;
        for (i = str.length - 1; i >= 0; i--) {
            ch = str.charCodeAt(i);
            hash ^= ((hash << 5) + ch + (hash >> 2));
        }
        return (hash & 0x7FFFFFFF);
    }


    function uploadError(file, error, message) {
        console.log("uploadError:" + message + '===' + error + file.name)
        // console.log("uploadError:" + error)
        // console.log("uploadError:" + error)
    }

    function fileDialogComplete() {

        this.startUpload();
        var settings = this.settings;
        settings.fileDialogComplete && settings.fileDialogComplete()
    }

    function uploadStart(file) {
        file = fileHandle(file)
        try {
            this.customSettings.progressCount = 0;
            updateDisplay.call(this, file);
        }
        catch (ex) {
            this.debug(ex);
        }
        file.size = Math.floor(file.size / 1024) + "KB";
        var settings = this.settings;
        settings.uploadStart && settings.uploadStart(file)
    }

    function uploadProgress(file, bytesLoaded, bytesTotal) {
        file = fileHandle(file)
        try {
            this.customSettings.progressCount++;
            updateDisplay.call(this, file);
        } catch (ex) {
            this.debug(ex);
        }
        var percent = bytesLoaded / bytesTotal * 100;
        var settings = this.settings;
        settings.uploadProgress && settings.uploadProgress(file, percent + "%")
    }

    function uploadSuccess(file, serverData) {
        file = fileHandle(file)
        try {
            updateDisplay.call(this, file);
        } catch (ex) {
            this.debug(ex);
        }
        serverData = eval("(" + serverData + ")");
        var settings = this.settings;
        settings.uploadSuccess && settings.uploadSuccess(file, serverData)
    }

    function uploadComplete() {

    }

    function fileQueued(file) {
        try {
            //var progress = new FileProgress(file, this.customSettings.progressTarget);
            //progress.setStatus("Pending...");
            //progress.toggleCancel(true, this);

        } catch (ex) {
            this.debug(ex);
        }

    }

    function fileQueueError(file, errorCode, message) {
        try {
            if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
                console.log("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
                return;
            }

            switch (errorCode) {
                case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                    console.log("error:File is too big.");
                    this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                    break;
                case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                    console.log("error:Cannot upload Zero Byte files.");
                    this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                    break;
                case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                    console.log("Error:Invalid File Type.");
                    this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                    break;
                default:
                    if (file !== null) {
                        console.log("Unhandled Error");
                    }
                    this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
                    break;
            }
        } catch (ex) {
            this.debug(ex);
        }
    }


    return function (opts) {
        var settings = {
            flash_url: "/Public/js/lib/SWFUpload/swfupload.swf",
            upload_url: "/json/package.json",
            //post_params: {"PHPSESSID": "083u52ntogrmcvf4i6blij29c7"},
            file_post_name: "file",
            use_query_string: true,
            file_size_limit: "1MB",
            file_types: "*.*",
            file_types_description: "All Files",
            file_upload_limit: 100,
            file_queue_limit: 0,
            button_cursor: SWFUpload.CURSOR.HAND,
            custom_settings: {
                progressTarget: "fsUploadProgress",
                cancelButtonId: "btnCancel"
            },
            debug: false,

            // Button settings
            button_image_url: "/Public/js/lib/swfupload/swfuploadImage_02.png",
            button_width: "112",
            button_height: "32",
            button_placeholder_id: "spanButtonPlaceHolder",


            file_dialog_complete_handler: fileDialogComplete,
            upload_start_handler: uploadStart,
            file_queued_handler: fileQueued,
            file_queue_error_handler: fileQueueError,
            upload_progress_handler: uploadProgress,
            upload_error_handler: uploadError,
            upload_success_handler: uploadSuccess,
            upload_complete_handler: uploadComplete
        };
        settings = $.extend(settings, opts)
        new SWFUpload(settings);
    }


})